package com.docman.controller;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.docman.domainmodel.DepartmentBean;
import com.docman.domainmodel.UniversityBean;
import com.docman.dao.DepartmentDAO;
import com.docman.dao.UniversityDAO;

@WebServlet("/getUniversities")
public class GetUniandDeptServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public GetUniandDeptServlet() {
        super();
        
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("text/html;  charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		
		ArrayList<UniversityBean> universities = new ArrayList<UniversityBean>();
		ArrayList<DepartmentBean> departments = new ArrayList<DepartmentBean>();
		
		universities=UniversityDAO.getUniversities();
		departments=DepartmentDAO.getDepartments();
		
		System.out.println(universities.toString()+"\n"+departments.toString()+"Hello from GetUniandDeptServlet");
		
		request.setAttribute("error", request.getAttribute("error"));
		request.setAttribute("uniList", universities);
		request.setAttribute("departmentList", departments);
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/register.jsp");
		 if (dispatcher != null){
			 
		  dispatcher.forward(request, response);

		  }
		 else{
			 System.out.println("Disp null");
		 }
	}

}
